/**
 * Copyright (c) 2011-9-23 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.wkm.definition;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wkm.frame.JDatePickerFrame;
import org.wkm.frame.MessageBox;
import org.wkm.task.file.DataReader;
import org.wkm.util.Home;
import org.wkm.util.SystemUtil;

/**
 * The class note
 * 
 * @version 1.0 2011-12-13
 * @author WUKUNMENG
 */
public class SystemTrayTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		new SystemTrayTest().test();
	}

	private TrayIcon trayIcon = null;
	private MenuItem addRegistry = null;

	// 系统配置文件
	private static DataReader READER = DataReader.getInstance();

	private static Logger logger = LoggerFactory
			.getLogger(SystemTrayTest.class);

	private SystemTray tray = null;
	
	private Image resizeImage = null;

	/**
	 * 程序最小化到托盘中
	 * 
	 * @throws AWTException
	 */
	public void test() throws AWTException {

		tray = SystemTray.getSystemTray();

		Image image = DataReader.getInstance().getImage(
				MessageBox.DEFAULT_IMAGE_NAME);
		resizeImage = image.getScaledInstance((int) tray
				.getTrayIconSize().getWidth(), (int) tray.getTrayIconSize()
				.getHeight(), BufferedImage.SCALE_SMOOTH);

		PopupMenu popup = new PopupMenu();
		MenuItem item = new MenuItem(
				READER.getProperty("system.tray.main.menu"));
		item.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
//				tray.remove(trayIcon);
				JDatePickerFrame.open();
//				try {
//					tray.add(trayIcon);
//				} catch (AWTException awt) {
//					// TODO Auto-generated catch block
//					awt.printStackTrace();
//					System.out.println(awt.getMessage());
//				}
			}

		});
		popup.add(item);

		if (SystemUtil.isWindows()
				&& SystemUtil.isOS32()) {
			if (RegistryUtil.isSetAutoRun(RegistryUtil.DEFAULT_KEY)) {
				addRegistry = new MenuItem(
						READER.getProperty("system.tray.auto.start.set"));
				logger.info("已设置了自启动！");
			} else {
				addRegistry = new MenuItem(
						READER.getProperty("system.tray.auto.start"));
				logger.info("未设置自启动！");
			}
			addRegistry.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					if (!RegistryUtil.isSetAutoRun(RegistryUtil.DEFAULT_KEY)) {
						try {
							File f = new File(Home.getPath("cmd.bat"));
							if (f.exists()) {
								RegistryUtil.setAutoRun(
										RegistryUtil.DEFAULT_KEY,
										f.getCanonicalPath());
								addRegistry.setLabel(READER
										.getProperty("system.tray.auto.start.set"));
								logger.info("注册自启动文件成功！");
							} else {
								logger.info("没有启动文件...");
							}
						} catch (IOException ex) {
							// TODO: handle exception
							ex.printStackTrace();
							logger.error(ex.getMessage());
						}
					} else {
						RegistryUtil.cancelAutoRun(RegistryUtil.DEFAULT_KEY);
						addRegistry.setLabel(READER
								.getProperty("system.tray.auto.start"));
						logger.info("取消自启动文件成功！");
					}
				}

			});

//			popup.add(addRegistry);
		}

		MenuItem exit = new MenuItem(READER.getProperty("system.tray.exit"));
		exit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				logger.info("系统正常退出！");
				System.exit(0);
			}

		});

		popup.add(exit);

		// 通过静态方法getSystemTray()得到系统托盘
		// 为这个托盘加一个提示信息
		// 装载托盘图象
		// 为这个托盘加一个弹出菜单
		StringBuffer sb = new StringBuffer();
		sb.append("WKM-定时关机管理器");
		trayIcon = new TrayIcon(resizeImage, sb.toString(), popup);
		tray.add(trayIcon);

		trayIcon.displayMessage("提示信息", "定时关机已经后台运行！", MessageType.INFO);
		trayIcon.addMouseListener(new MouseListener() {

			public void mouseClicked(MouseEvent e) {
				if(!JDatePickerFrame.visible() && MouseEvent.BUTTON1 == e.getButton()){
					JDatePickerFrame.open();
				}
				// TODO Auto-generated method stub
				// trayIcon.displayMessage("提示信息", "定时工具后台运行！",
				// MessageType.INFO);
			}

			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				logger.info("鼠标移入！");
				trayIcon.displayMessage("提示信息", "定时工具后台运行！", MessageType.INFO);
			}

			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				logger.info("鼠标移出！");
			}

			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
			}

			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			}

		});
		
//		Thread t = new Thread(new My());
//		t.start();
		
	}
	
	class My implements Runnable{

		private Image img = Toolkit.getDefaultToolkit().getImage("");
		public void run() {
			// TODO Auto-generated method stub
			while (true){
				try {
					trayIcon.setImage(img);
					Thread.sleep(300);
					trayIcon.setImage(resizeImage);
					Thread.sleep(300);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
			
		}
		
	}
}


